import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;


public class Leetcode219 {

    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        HashSet<Integer> set = new HashSet<>(k);
        for (int i = 0; i < nums.length; i++) {
            //创建窗口并维护是否重复
            if (!set.add(nums[i])) {
                return true;
            }

            //窗口滑动
            if (set.size() > k) {
                set.remove(nums[i - k]);
            }
        }

        return false;
    }
}
